<?php
namespace app\system\controller;

use app\common\model\AuthGroupModel;
use app\common\model\UserModel;
use think\Request;

class AdminController extends BaseController
{
    private $userModel;
    private $authGroupModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel      = new UserModel();
        $this->authGroupModel = new AuthGroupModel();
    }

    /**
     * 会员
     * @return mixed
     */
    public function index()
    {
        $map          = [];
        $query        = $this->request->request();
        $searchKey    = $this->request->param('searchKey');
        $searchText   = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) $map[$searchKey]     = ['like', '%' . $searchText . '%'];
        $list = $this->userModel->where($map)->order('uid desc')
            ->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try{
                $data   = $this->request->param();
                $data['utype'] = $this->getUtype();
                $result = $this->validate($data, [
                    'username|用户名' => 'require',
                    'password|密码' => 'require',
                    'name|姓名'     => 'require',
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                $result = $this->userModel->addData($data);
                if($result===false){
                    exception($this->userModel->getError(),40001);
                }
            } catch (\Exception $e){
                $this->error($e->getMessage());             
            }
            $this->success('添加成功！',url('index'));
        }
        $groupList = $this->authGroupModel->where('utype', $this->getUtype())->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            try{
                $data   = $this->request->param();
                $data['utype'] = $this->getUtype();
                $result = $this->validate($data, [
                    'username|用户名' => 'require',
                    'name|姓名'     => 'require',
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                $this->userModel->editData($data);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('修改成功！', url('index'));
        }
        $uid       = $this->request->param('uid');
        $info      = $this->userModel->get($uid);
        $groupList = $this->authGroupModel->where('utype', $this->getUtype())->order('group_sort asc')->select();
        $this->assign('groupList', $groupList);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 禁用启用
     */
    public function setStatus()
    {
        try{
            $uid = $this->request->param('uid', 0);
            $this->userModel->setStatus($uid);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('操作成功！');

    }

    /**
     * 删除用户
     */
    public function delUser(){
        try{
            $uid = $this->request->param('uid', 0);
            if($uid>0){
                $this->userModel->where('uid',$uid)->delete();
            }
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('操作成功！');
    }

    /**
     * 修改密码
     */
    public function editpass()
    {
        if ($this->request->isAjax()) {
            try{
                $oldPassword     = $this->request->param('old_password');
                $newPassword     = $this->request->param('new_password');
                $replayPassword  = $this->request->param('replay_password');
                $map['uid']      = $this->getUid();
                $map['password'] = md5($oldPassword);
                if ($this->userModel->where($map)->find()) {
                    if ($newPassword == $replayPassword) {
                        $data['uid']      = $this->getUid();
                        $data['password'] = $newPassword;
                        $this->userModel->editData($data);
                        cookie('systemDes', null);
                    } else {
                        exception('原密码和确认密码不一致！',40001);
                    }
                } else {
                    exception('原密码输入错误！',40001);
                }
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('密码修改成功,请重新登陆！', url('editpass'));
        }
        $info = $this->userModel->get($this->getUid());
        $this->assign('info', $info);
        return $this->fetch();
    }

}
